﻿/*
 *********************************************************************
 *    Management Extensions for Windows Windows Server AppFabric     *
 *********************************************************************
 *    Author    : Fatih Boy [fatih@enterprisecoding.com]             *
 *    Web site  : http://www.enterprisecoding.com                    *
 *********************************************************************
 *    License   : GNU Library General Public License (LGPL)          * 
 *********************************************************************
 */

using System.Collections;

using Com.Enterprisecoding.AppFabric.Extensions.Powershell;
using Microsoft.Web.Management.Server;

namespace Com.Enterprisecoding.AppFabric.Extensions.Services {
    internal sealed class CacheAccountService : ChildServiceBase {
        [ModuleServiceMethod(PassThrough = true)]
        public ArrayList GetAllowedClientAccounts() {
            var accountList = new ArrayList();
            var host = new PowershellHost();

            host.AddCommand("Use-CacheCluster");
            host.AddCommand("Get-CacheAllowedClientAccounts");

            var result = host.Invoke();

            for (int i = 0; i < result.Count; i++) {
                var acccountBag = new PropertyBag();
                acccountBag[0]= result[i].BaseObject;

                accountList.Add(acccountBag);
            }

            return accountList;
        }

        [ModuleServiceMethod(PassThrough = true)]
        public void GrantCacheAllowedClientAccount(string accountName, bool force) {
            var host = new PowershellHost();

            host.AddCommand("Use-CacheCluster");
            host.AddCommand("Grant-CacheAllowedClientAccount");
            host.AddParameter("Account", accountName);

            if (force) {
                host.AddParameter("Force");
            }

            host.Invoke();
        }

        [ModuleServiceMethod(PassThrough = true)]
        public void RevokeCacheAllowedClientAccount(string accountName) {
            var removeAccountHost = new PowershellHost();

            removeAccountHost.AddCommand("Use-CacheCluster");
            removeAccountHost.AddCommand("Revoke-CacheAllowedClientAccount");
            removeAccountHost.AddParameter("Account", accountName);
            removeAccountHost.Invoke();
        }
    }
}